using UnityEngine;
using System.Collections;

/**
 * All the player persistent state should go here
 * e.g. items, money, etc.
 * Anything that needs to be saved
 */
public class PlayerPersistence : MonoBehaviour {
	public Inventory inventory;
	public AccountCurrency currency;
	
	const bool DEBUG = true;
	
	void Awake () {
		this.inventory = new Inventory();
		this.currency = new AccountCurrency();
		
		this.currency.AddAmount(new GoldCurrency(5000));
		this.currency.AddAmount(new NyotaCurrency(5000));
	}
	
	void Update () {	
	}
	
	/* TODO: throw exceptions for failure cases */
	public bool BuyItemStack(ItemShop shop, ItemStack s, PriceCurrency p) {
		/* TODO: ignore cost for now */
		
		if (PlayerPersistence.DEBUG) {
			Debug.Log ("buying " +
				s.ToString() + " for " +
				p.ToString() +
				" at " + shop.ToString ());
		}
		
		if (!this.currency.CanAfford (p)) {
			if (PlayerPersistence.DEBUG) {
				Debug.Log ("not enough money for " + s.ToString ());
			}
			return false;
		}
		
		if (!this.inventory.HasSpaceForItemStack(s)) {
			if (PlayerPersistence.DEBUG) {
				Debug.Log ("no space for " + s.ToString ());
			}
			return false;
		}
		
		/**
		 * Do checks first before doing anything permanent
		 * Also this would have to be atomic and/or need an escrow-type system for mmo
		 */
		this.currency.SubtractAmount(p);		
		
		return this.inventory.AddItemStack(s);
	}
}
